package com.zryc.shop.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.entity.Coupon;
import com.zryc.shop.sms.entity.CouponProductCategoryRelation;
import com.zryc.shop.sms.entity.CouponProductRelation;
import com.zryc.shop.sms.entity.FlashPromotion;
import com.zryc.shop.sms.mapper.CouponMapper;
import com.zryc.shop.sms.service.ICouponProductCategoryRelationService;
import com.zryc.shop.sms.service.ICouponProductRelationService;
import com.zryc.shop.sms.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired
    private ICouponProductRelationService couponProductRelationService;

    @Autowired
    private ICouponProductCategoryRelationService couponProductCategoryRelationService;


    /**
     * 优惠券列表
     *
     * @param name
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Coupon> list(String name, Integer type, Integer pageNum, Integer pageSize) {
        Page<Coupon> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (type != null) {
            wrapper.eq("type", type);
        }
        return page(page, wrapper);
    }


    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Object deleteCoupon(Long id) {
        baseMapper.deleteById(id);//优惠券表
        baseMapper.deleteFormCouPro(id);
        baseMapper.deleteFromCouProCat(id);
        return null;
    }

    /**
     * 更新优惠券
     *
     * @param id
     * @param coupon
     * @return
     */
    @Override
    @Transactional
    public Object updateCoupon(Long id, Coupon coupon) {
        UpdateWrapper<Coupon> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        baseMapper.update(coupon,updateWrapper);

        baseMapper.deleteFormCouPro(id);
        baseMapper.deleteFromCouProCat(id);

        List<CouponProductRelation> productRelationList = coupon.getProductRelationList();
        for (CouponProductRelation productRelation : productRelationList) {
            productRelation.setCouponId(coupon.getId());
        }
        couponProductRelationService.saveBatch(productRelationList);

        List<CouponProductCategoryRelation> productCategoryRelationList = coupon.getProductCategoryRelationList();
        for (CouponProductCategoryRelation productCategoryRelation : productCategoryRelationList) {
            productCategoryRelation.setCouponId(coupon.getId());
        }
        couponProductCategoryRelationService.saveBatch(productCategoryRelationList);
        return null;
    }

    /**
     * 添加优惠券
     *
     * @param coupon
     * @return
     */
    @Override
    @Transactional
    public Object create(Coupon coupon) {
        super.save(coupon);
        List<CouponProductRelation> productRelationList = coupon.getProductRelationList();
        for (CouponProductRelation productRelation : productRelationList) {
            productRelation.setCouponId(coupon.getId());
        }
        couponProductRelationService.saveBatch(productRelationList);

        List<CouponProductCategoryRelation> productCategoryRelationList = coupon.getProductCategoryRelationList();
        for (CouponProductCategoryRelation productCategoryRelation : productCategoryRelationList) {
            productCategoryRelation.setCouponId(coupon.getId());
        }
        couponProductCategoryRelationService.saveBatch(productCategoryRelationList);
        return "创建成功";
    }

    /**
     * 查询优惠券信息
     *
     * @param id
     * @return
     */
    @Override
    public Object detail(Long id) {
        Coupon coupon = baseMapper.selectById(id);
        QueryWrapper<CouponProductCategoryRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", id);
        coupon.setProductCategoryRelationList(couponProductCategoryRelationService.list(queryWrapper));
        QueryWrapper<CouponProductRelation> wrapper= new QueryWrapper<>();
        wrapper.eq("coupon_id", id);
        coupon.setProductRelationList(couponProductRelationService.list(wrapper));
        return coupon;
    }
}
